import React, { Component } from 'react';
import { WeaTableNew } from 'comsMobx';
import { WeaDialog, WeaLocaleProvider, WeaTools } from 'ecCom';

const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;
const TableStore = WeaTableNew.TableStore;

export default class OpenModelCompenent extends Component {
  iseditmould = false;

  constructor(props) {
    super(props);
    this.state = {
      tableStore: new TableStore(),
    };
  }

  componentDidMount() {
    // const workflowid = Util.getQueryString("workflowid");
    // const selectvalue = Util.getQueryString("selectvalue");
    // const iseditmould = Util.getQueryString("iseditmould") === "false" ? false : true;
    const openModelComponent = document.getElementById('openModelComponent');
    const workflowid = openModelComponent.getAttribute('workflowId');
    const requestId = openModelComponent.getAttribute('requestId');
    const selectvalue = openModelComponent.getAttribute('selectvalue');
	const editMouldfieldidValue = openModelComponent.getAttribute("editMouldfieldidValue");
    const iseditmould = openModelComponent.getAttribute('iseditmould') != 'false';
    this.iseditmould = iseditmould;
    const params = {
      selectvalue,
      workflowid,
      iseditmould,
      requestId,
	  editMouldfieldidValue,
    };
    WeaTools.callApi('/api/odoc/odocSelectMould/getMouldList', 'POST', params).then((data) => {
      if (data.api_status) {
        this.state.tableStore.getDatas(data.sessionkey);
      }
    });
  }

  render() {
    const { tableStore } = this.state;
    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@kulx2f`}
        onCancel={this.removeDialog}
        title={getLabel('23039', '选择模板')}
        visible={true}
        style={{ width: '35%', height: '500px' }}
        hasScroll
        parentClassName="openModelComponent"
      >
        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@lfths0`}
          comsWeaTableStore={tableStore}
          getColumns={this.getColumns}
        />
      </WeaDialog>
    );
  }

  removeDialog = () => {
    const isColumnShow = WfForm.getOdocConfig().docParam.isColumnShow;
    const isTextInFormCanEdit = WfForm.getOdocConfig().docParam.isTextInFormCanEdit;
    const isTextInForm = WfForm.getOdocConfig().docParam.isTextInForm;
    const docid = WfForm.getOdocConfig().docParam.docid;

    if (isTextInFormCanEdit && (isColumnShow == '1' || isTextInForm) && docid > 0) {
      if (isColumnShow == '1') {
        document.getElementsByClassName('wf-req-form-odoc-iframe')[0].style.width = '100%';
      } else {
        document.getElementById('requestdocInForm').style.width = '100%';
      }
    } else {
      document.getElementsByClassName('req-workflow-odoc')[0].style.width = '100%';
    }
    showOrHideWeboffice(1);
    showOrHideTextInForm(false);
    const openVersionComponent = document.getElementsByClassName('openModelComponent')[0];
    const iframe = document.getElementById('openModelComponent');
    iframe.parentNode.removeChild(iframe);
    openVersionComponent.parentNode.removeChild(openVersionComponent);
  }

  getColumns = columns => columns.map((col) => {
    if (col.dataIndex == 'mouldName') {
      return {
        ...col,
        title: col.title,
        className: 'customWrap',
        render: (text, record) => (
          <a onClick={() => this.openModel(record.id)}>{record.mouldName}</a>
        ),
      };
    }
    return col;
  })

  openModel(modelId) {
    let iframeWindow;
    const isColumnShow = WfForm.getOdocConfig().docParam.isColumnShow;
    const isTextInFormCanEdit = WfForm.getOdocConfig().docParam.isTextInFormCanEdit;
    const isTextInForm = WfForm.getOdocConfig().docParam.isTextInForm;
    const docid = WfForm.getOdocConfig().docParam.docid;

    if (isTextInFormCanEdit && (isColumnShow == '1' || isTextInForm) && docid > 0) {
      if (isColumnShow == '1') {
        iframeWindow = document.getElementsByClassName('wf-req-form-odoc-iframe')[0].contentWindow;
      } else {
        iframeWindow = document.getElementById('requestdocInForm').contentWindow;
      }
    } else {
      iframeWindow = document.getElementsByClassName('req-workflow-odoc')[0].contentWindow;
    }
    if (this.iseditmould) {
      iframeWindow.afterSelectEditMould(undefined, { id: modelId });
    } else {
      iframeWindow.afterSelectMould(undefined, { id: modelId });
    }
    this.removeDialog();
  }
}
